package com.mr.cai.main.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;

import com.cai.mr.lib.utils.LogInfo;
import com.mr.cai.main.R;
import com.mr.cai.main.adapter.GroupFragmentAdapter;
import com.mr.cai.main.bean.GroupInfoBean;
import com.mr.cai.main.utils.CommonCoreUtil;

import java.util.ArrayList;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by hai8108 on 16/2/10.
 */
public class GroupPageFragment extends BaseFragment {
    private GridView groupGridView;
    private GroupFragmentAdapter adapter;
    private EditText searchEd;
    private ArrayList<GroupInfoBean> groupInfoBeans;

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mPublicLayout = CommonCoreUtil.createPage(getActivity(), R.layout
                .fragment_page_group_layout);
        return mPublicLayout;
    }

    @Override
    public void onViewCreated (View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        findView();
        setListener();
        requestGroupData("groupId");
    }

    private void setListener() {
        groupGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                try {
                    GroupInfoBean groupInfoBean= (GroupInfoBean) adapterView.getItemAtPosition(i);
                    if(groupInfoBean==null){
                        return;
                    }

                }catch (Exception e){

                }

            }
        });
    }

    private void findView () {
        searchEd = (EditText) mPublicLayout.findViewById(R.id.mr_cai_search);
        CommonCoreUtil.setClickAction(searchEd, new Action1() {
            @Override
            public void call (Object o) {
                CommonCoreUtil.showToast("To search page!");
            }
        });
        groupGridView = (GridView) mPublicLayout.findViewById(R.id.mr_group_gridview);
        adapter = new GroupFragmentAdapter(getActivity());
        groupGridView.setAdapter(adapter);

    }

    @Override
    public void onActivityCreated (Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private Subscription groupSubscription;

    private void requestGroupData (String groupId) {
        Observable.just(groupId).subscribeOn(Schedulers.io()).map(new Func1<String,
                ArrayList<GroupInfoBean>>() {
            @Override
            public ArrayList<GroupInfoBean> call (String s) {
                groupInfoBeans = new ArrayList<>();
                GroupInfoBean mGroupBean = null;
                for (int i = 0; i < 10; i++) {
                    mGroupBean = new GroupInfoBean();
                    mGroupBean.setgChinaName("菜先生---" + i);
                    mGroupBean.setgEngName("MrCai---" + i);
                    groupInfoBeans.add(mGroupBean);
                }
                return groupInfoBeans;
            }
        }).doOnSubscribe(new Action0() {
            @Override
            public void call () {
                mPublicLayout.loading(true);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<GroupInfoBean>>() {
                    @Override
                    public void call (ArrayList<GroupInfoBean> groupInfoBeans) {
                        mPublicLayout.finish();
                        adapter.setList(groupInfoBeans);
                        LogInfo.log("haitian", "groupInfoBeans.size ="+groupInfoBeans.size());
                    }
                });
//        Observable<GroupInfoBeanList> authCodeObservable = MrCaiHttpApi.requestGroupList(groupId, new GroupInfoBeanListParse());
//        groupSubscription = authCodeObservable.observeOn(AndroidSchedulers.mainThread())
//                .doOnSubscribe(new Action0() {
//                    @Override
//                    public void call () {
//                        mPublicLayout.loading(true);
//                    }
//                })
//                .subscribe(new Subscriber<GroupInfoBeanList>() {
//                    @Override
//                    public void onCompleted () {
//                        mPublicLayout.finish();
//                    }
//                    @Override
//                    public void onError (Throwable e) {
//                        mPublicLayout.dataError(false);
//                    }
//                    @Override
//                    public void onNext (GroupInfoBeanList mGroupInfoBeanList) {
//                        if(mGroupInfoBeanList.isResult() && mGroupInfoBeanList.getCode() == 0){
//                            adapter.setGroupInfoData(mGroupInfoBeanList.getGroupInfoBeans());
//                        } else {
//                            mPublicLayout.dataNull(mGroupInfoBeanList.getMsg());
//                        }
//                    }
//                });
    }

    @Override
    public void onDestroy () {
        super.onDestroy();
        if (groupSubscription != null) {
            groupSubscription.unsubscribe();
            groupSubscription = null;
        }
    }
}
